package trying;

import java.awt.event.ItemEvent;
import java.util.*;

/**
 * 取随机数的速度测试，小数据量map快，大数据量数组快，shuffle最快
 */
public class RandomTest {
    public static void main(String[] args) {
        ArrTest();
    }

    public static void SetTest() {
        System.out.println(System.currentTimeMillis());
        int list_count = 10000000;
        int count = 9000000;
        List list = new ArrayList<>();
        if (count >= list_count || count < 0) {
            return;
        }
        Random random = new Random();
        Set set = new HashSet();
        while (set.size() < count) {
            int temp = random.nextInt(list_count);
            set.add(temp);
            list.add(temp);
        }
        System.out.println(list.size());
        System.out.println(System.currentTimeMillis());
    }

    public static void MapTest() {
        System.out.println(System.currentTimeMillis());
        int list_count = 10000000;
        int count = 9000000;
        List list = new ArrayList<>();
        if (count >= list_count || count < 0) {
            return;
        }
        Random random = new Random();
        Map<Integer,Boolean> map = new HashMap(list_count);
        while (true) {
            if (map.size() == count) {
                break;
            }
            int temp = random.nextInt(list_count);
            if (map.get(temp) == null) {
                map.put(temp,true);
                list.add(temp);
            }
        }
        System.out.println(list.size());
        System.out.println(System.currentTimeMillis());
    }

    public static void MapTest2() {
        System.out.println(System.currentTimeMillis());
        int list_count = 10000;
        int count = 9000;
        List list = new ArrayList<>();
        if (count >= list_count || count < 0) {
            return;
        }
        Random random = new Random();
        Map map = new HashMap();
        while (map.size() < count) {
            int temp = random.nextInt(list_count);
            if (!map.containsKey(temp)) {
                map.put(temp,null);
                list.add(temp);
            }
        }
        System.out.println(list.size());
        System.out.println(System.currentTimeMillis());
    }

    public static void ArrTest() {
        System.out.println(System.currentTimeMillis());
        int list_count = 10000;
        int count = 9000;
        List list = new ArrayList<>();
        if (count >= list_count || count < 0) {
            return;
        }
        Random random = new Random();
        boolean[] boo = new boolean[list_count];
        int n = 0;
        while (n < count) {
            int temp = random.nextInt(list_count);
            if (!boo[temp]) {
                n++;
                boo[temp] = true;
                list.add(temp);
            }
        }
        System.out.println(list.size());
        System.out.println(System.currentTimeMillis());
        shuffle(list);
    }

    public static void shuffle(List list) {
        System.out.println(System.currentTimeMillis());
        int count = 9000;
        if (count > list.size()) {
            throw new RuntimeException();
        }
        Collections.shuffle(list);
        List list2 = list.subList(0,count);
        System.out.println(list2.size());
        System.out.println(System.currentTimeMillis());
    }
}
